<template>
  <div class="cate">
    <!-- 搜索 -->
    <search></search>

    <!-- 分类 -->
    <div class="cate-main">
      <scroll-view scroll-y class="cate-left">
        <block v-for="(item, index) in cateList" :key="item.cat_id">
          <div @tap="tapLeftItem(index)" class="left-item" :class="{'active': index === tabIndex}">{{item.cat_name}}</div>
        </block>
      </scroll-view>
      <scroll-view scroll-y class="cate-right">
        <block v-for="(citem, index) of rightData" :key="index">
          <div class="content">
            <div class="content-title">{{citem.cat_name}}</div>
            <div class="content-list">
              <block v-for="(subItem, subIndex) of citem.children" :key="subIndex">
                <div class="item" @tap="tapRightItem(subItem)">
                  <!-- :src="'https://itjustfun.cn/' + subItem.cat_icon" -->
                  <image
                    class="item-img"
                    :src="subItem.cat_icon"
                    mode="aspectFill"
                    lazy-load="false">
                  </image>
                  <div class="item-desc">{{subItem.cat_name}}</div>
                </div>
              </block>
            </div>
          </div>
        </block>
      </scroll-view>
    </div>
  </div>
</template>

<script>
import Search from '@/components/Search'
import { getCate } from '@/api'
export default {
  components: {
    Search
  },
  data () {
    return {
      cateList: [], // 分类列表
      tabIndex: 0, // 选中的
      rightData: [] // 右边渲染的数据
    }
  },
  mounted () {
    // 初始化页面数据
    this.initPage()
  },
  methods: {
    // 初始化页面数据
    initPage () {
      getCate().then(res => {
        // console.log('cate', res)
        if (res.meta.status === 200) {
          this.cateList = res.message
          this.rightData = this.cateList[this.tabIndex].children
        }
      })
    },
    // 点击
    tapLeftItem (index) {
      this.tabIndex = index
      this.rightData = [] // 将其置空，然后再对其赋值
      // 连续对同一个值做赋值操作，会被优化策略省略，需要将其放入延时函数中即可
      setTimeout(() => {
        this.rightData = this.cateList[this.tabIndex].children
      }, 0)
    },
    // 点击右边
    tapRightItem (subItem) {
      wx.navigateTo({ url: '/pages/goodslist/main?query=' + subItem.cat_name + "&cid=" + subItem.cat_id});
    }
  }
}
</script>

<style lang="scss" scoped>
.cate-main {
  display: flex;
  position: fixed;
  left: 0;
  top: 100rpx;
  right: 0;
  bottom: 0;
  color: #666;
  // 左
  .cate-left {
    width: 200rpx;
    height: 100%;
    background-color: #f8f8f8;
    .left-item {
      position: relative;
      padding: 20rpx;
      height: 50rpx;
      line-height: 50rpx;
      font-size: 26rpx; 
    }
    .left-item.active {
      color: #ff2d49;
      background-color: #fff;
      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 15rpx;
        bottom: 15rpx;
        width: 10rpx;
        background-color: #ff2d49;
      }
    }
  }
  // 右
  .cate-right {
    flex: 1;
    .content {
      .content-title {
        height: 80rpx;
        line-height: 80rpx;
        padding: 0 50rpx;
        text-align: center;
        font-size: 30rpx;
        font-weight: bold;
        color: #ff2d49;
        &::before, &::after {
          content: '/';
          padding: 0 50rpx;
          color: #ddd;
        }
      }
      .content-list {
        display: flex;
        flex-wrap: wrap;
        .item {
          width: 33.33%;
          padding: 20rpx 0;
          text-align: center;
          .item-img {
            width: 120rpx;
            height: 100rpx;
          }
          .item-desc {
            font-size: 26rpx;
          }
        }
      }
    }
  }
}

</style>

